package com.usian.service.impl;





import com.usian.config.SmsConfig;
import com.usian.realname.Realname;
import com.usian.square.Square;
import com.usian.mapper.UserMapper;
import com.usian.user.User;
import com.usian.vo.Rs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;


@Service
public class UserServiceImpl implements UserService{


     @Autowired
     private UserMapper userMapper;



    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    public Rs sendCode(String account, String code) throws Exception {
        //生成随机数
        // 创建一个Random对象
        Random random = new Random();
        // 生成一个1000到9999之间的随机数
        int smsCode = 1000 + random.nextInt(9000);
//        log.info("生成随机数:{}", code);
        //发送短信
        SmsConfig.sendSms(account,smsCode + "");
        return Rs.ok().put("code", smsCode);
    }

    @Override
    public Integer updateUserLucky(Integer id, Integer charisma) {
        return userMapper.UserLucky(id,charisma);
    }


    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public int userInfoEdit(User user) {
        return userMapper.userInfoEdit(user);
    }

    @Override
    public List<Square> selectUserPost(Integer id) {
        List<Square> squares = userMapper.selectUserPost(id);
        return squares;
    }

    @Override
    public Integer selectUserMypost(Integer id) {
        return userMapper.selectUserMypost(id);
    }

    @Override
    public List<Realname> realname(String card) {
        return userMapper.realname(card);
    }

    @Override
    public void saverealname(Integer userId, String realname, String card) {
        // 调用userMapper的savecertified方法保存实名认证信息
        userMapper.saverealname(userId, realname, card);

        // 更新已认证的用户信息，通过真实姓名获取更新后的用户ID
        Integer id = userMapper.updaterealnameById(card);

        // 根据用户ID查询用户信息
        User user = userMapper.selectById(userId);

        // 更新用户认证状态，这里的id是通过真实姓名获取的更新后的用户ID
        userMapper.updateById(user.getId(),id);
    }

    @Override
    public Realname selectRealname(Integer id) {
        return userMapper.selectRealname(id);
    }

    @Override
    public List<User> selectUserLucky(Integer id) {
        return userMapper.selectUserLucky(id);
    }

    @Override
    public Integer selectUserMyLucky(Integer id) {
        return userMapper.selectUserMyLucky(id);
    }

    @Override
    public Integer selectCharisma(Integer id) {
        return userMapper.selectCharisma(id);
    }

    @Override
    public User logon(User user) {
        return userMapper.logon(user);
    }



    @Override
    public int deletePost(Integer id) {
        return userMapper.deletePost(id);
    }







}





